// index.js
// 获取应用实例
const app = getApp()
const dayjs = require("./dayjs");
var WxNotificationCenter = require("../../config/WxNotificationCenter");

Page({
  data: {
    swiperList: [{
        id: 0,
        type: 'image',
        url: '../../../../static/images/banner1.jpeg'
      },
      {
        id: 4,
        type: 'image',
        url: '../../../../static/images/banner2.jpeg'
      }
    ],
    funList: [{
        title: '会议室预约',
        subTitle: '今日空闲30个',
        imgUrl: '../../static/images/index/home_fun1.png',
        router: '/pages/choosingmeeting/choosingmeeting'
      },
      {
        title: '车辆预约',
        subTitle: '今日空闲2台',
        imgUrl: '../../static/images/index/home_fun2.png',
        router: '/pages/vehicle/appointment/appointment'
      },
      {
        title: '工作任务',
        subTitle: '有新任务',
        imgUrl: '../../static/images/index/home_fun5.png',
        router: ''
      },
      {
        title: '外卖',
        subTitle: '安心好吃实惠',
        imgUrl: '../../static/images/index/home_fun3.png',
        router: '/pages/takeOut/list/takeOutList'
      },
      {
        title: '预约记录',
        subTitle: '点击查看预约',
        imgUrl: '../../static/images/index/home_fun4.png',
        router: '/pages/bookinghistory/bookinghistory'
      },
      {
        title: '领导邮箱',
        subTitle: '吐槽评论',
        imgUrl: '../../static/images/index/home_fun6.png',
        router: '/pages/forumList/forumList'
      },
    ],
    userInfo: {},
    userName: '请登录',
    dayMark: '一', // 星期几
    day: '', // 日期
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },

  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

    // 计算首页日期
    this.dayMarkFun()


    let that = this;
    WxNotificationCenter.addNotification("loginsuccess", that.getuserinfo, that);
    this.getuserinfo();
  },
  getuserinfo() {

    let userInfo = app.globalData.userInfo;
    console.log(userInfo, 'indexpage');
    if (userInfo) {
      this.setData({
        userInfo: userInfo,
        userName: userInfo.name
      })
    }
    console.log('userInfo:', this.data.userInfo)

  },
  getUserProfile(e) {
    if (!this.data.canIUseGetUserProfile || !!this.data.userInfo && !!this.data.userInfo.avatarUrl) {
      return
    }
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('res:', res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          userName: res.userInfo.nickName
        })
        wx.setStorage({
          data: res.userInfo, //需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify序列化的对象。
          key: 'userInfo', //本地缓存中指定的 key
        })
      }
    })
  },
  getauto() {

  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  gotologin() {
    if (app.globalData.islogin) {
      return false;
    }
    wx.navigateTo({
      url: '../loginpage/loginpage',
    })
    return true;
  },
  // 跳转页面
  goRouter(e) {
    // 判断登录
    if (this.gotologin()) {
      return;
    }
    let router = e.currentTarget.dataset.router
    let name = e.currentTarget.dataset.name
    // 检测权限
    if (router && app.checkFun(name)) {
      // if (router) {
      wx.navigateTo({
        url: router,
      })
    }
  },
  // 计算当前星期几
  dayMarkFun() {
    switch (dayjs().day()) {
      case 0:
        this.dayMark = '日'
        break;
      case 1:
        this.dayMark = '一'
        break;
      case 2:
        this.dayMark = '二'
        break;
      case 3:
        this.dayMark = '三'
        break;
      case 4:
        this.dayMark = '四'
        break;
      case 5:
        this.dayMark = '五'
        break;
      case 6:
        this.dayMark = '六'
        break;
    }
    this.setData({
      dayMark: this.dayMark,
      day: dayjs().format('MM') + '月' + dayjs().format('DD') + '日'
    })

  }
})